package com.huirui.server.controller.mini.program;

import com.huirui.server.pojo.dto.ShippingCartDto;
import com.huirui.server.pojo.dto.ShippingCartListDto;
import com.huirui.server.pojo.result.Result;
import com.huirui.server.pojo.vo.ShippingCartVo;
import com.huirui.server.service.ShippingCartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/shipping-cart")
@Slf4j
@Validated
@Tag(name = "购物车管理", description = "微信小程序用户购物车相关")
public class ShippingCartController {

    @Resource
    private ShippingCartService shippingCartService;

    @PostMapping
    @Operation(summary = "添加商品到购物车")
    public Result<Void> addShippingCart(@Valid @RequestBody ShippingCartDto shippingCartDto) {
        shippingCartService.saveShippingCart(shippingCartDto);
        return Result.success();
    }

    @GetMapping
    @Operation(summary = "查询用户所有的购物车")
    public Result<List<ShippingCartVo>> listUserShippingCart() {
        List<ShippingCartVo> list = shippingCartService.listUserShippingCart();
        return Result.success(list);
    }

    @DeleteMapping
    @Operation(summary = "删除购物车")
    public Result<Void> deleteShippingCart(@RequestParam("ids") List<Long> ids) {
        shippingCartService.removeByIds(ids);
        return Result.success();
    }

    @PutMapping("/update/{shipping-cart-id}")
    @Operation(summary = "修改购物车中的商品数量")
    public Result<Void> updateCartItemQuantity(@PathVariable("shipping-cart-id") Long id, @RequestParam("newQuantity") Long newQuantity) {
        shippingCartService.updateCartItemQuantity(id, newQuantity);
        return Result.success();
    }

    @GetMapping("/discount")
    @Operation(summary = "获取所有已经降价的商品")
    public Result<List<ShippingCartVo>> listDiscountProducts() {
        List<ShippingCartVo> list = shippingCartService.listDiscountProducts();
        return Result.success(list);
    }


    @PostMapping("/remove2favorite")
    @Operation(summary = "将商品移入收藏")
    public Result<Void> removeShippingCartToFavorite(@RequestParam("ids") List<Long> ids) {
        shippingCartService.removeShippingCartToFavorite(ids);
        return Result.success();
    }


    @PostMapping("/batch/save")
    @Operation(summary = "批量添加商品到购物车")
    public Result<Void> batchSaveShippingCart(@RequestBody @Valid ShippingCartListDto shippingCartListDto) {
        shippingCartService.batchSaveShippingCart(shippingCartListDto);
        return Result.success();
    }

    @GetMapping("/count")
    @Operation(summary = "查询购物车商品数量")
    public Result<Long> countShippingCart() {
        Long count = shippingCartService.countShippingCart();
        return Result.success(count);
    }


}
